#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

//*length 为字符数组string的总容量*/
void Merge(char str1[], int len1, char str2[], int len2)
{
    if(str1 == NULL && len1 <= 0)
        return;

    /*originalLength 为字符串str1的实际长度*/
    int originalLength1 = 0;
    int originalLength2 = 0;
    int i = 0;
    for(int i=0; str1[i]!='\0'; ++i)
    {
    	++ originalLength1;
    }
    for(int i=0; str2[i]!='\0'; ++i)
    {
    	++ originalLength2;
    }
	int totalLength = originalLength1 + originalLength2;
    if(totalLength > len1-1)
        return;

    int indexOfStr1 = totalLength;
    int indexOfStr2 = originalLength2;
    while(indexOfStr2>=0)
    {
    	str1[indexOfStr1] = str2[indexOfStr2];
    	-- indexOfStr1;
    	-- indexOfStr2;
    }
    
}

void Test(char* testName, char str1[], int len1, char str2[], int len2, char expected[])
{
    if(testName != NULL)
        printf("%s begins: ", testName);

    Merge(str1, len1, str2, len2);

    if(expected == NULL && str1 == NULL)
        printf("passed.\n");
    else if(expected == NULL && str1 != NULL)
        printf("failed.\n");
    else if(strcmp(str1, expected) == 0)
        printf("passed.\n");
    else
        printf("failed.\n");
}

// 空格在句子中间
void Test1()
{
    const int length = 100;

    char str1[length] = "hello world";
    char str2[100] = "Jiang kun";
    Test("Test1", str1, length, str2, 100, "hello worldJiang kun");
}


// 传入NULL
void Test5()
{
    Test("Test5", NULL, 0, NULL, 0, NULL);
}

// 传入内容为空的字符串
void Test6()
{
    const int length = 100;

    char string[length] = "";
    char str2[20] = "Jiang kun";
    Test("Test6", string, length,str2, 20, "Jiang kun");
}


int main()
{
    Test1();
   
    Test5();
    Test6();


    return 0;
}

